package cn.edu.scau.cmi.wujiaxian.exercise11;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class WindowTriangle extends JFrame implements ActionListener{
	private static final long serialVersionUID = 4174621914605686809L;

	Triangle triangle;
	JTextField textFieldA;
	JTextField textFieldB;
	JTextField textFieldC;
	JTextArea showArea;
	JButton controlButton;
	
	public WindowTriangle() {
		init();
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	void init() {
		triangle = new Triangle();
		textFieldA = new JTextField(5);
		textFieldB = new JTextField(5);
		textFieldC = new JTextField(5);
		showArea = new JTextArea();
		controlButton = new JButton("计算面积");
		JPanel pNorth = new JPanel();
		pNorth.add(new JLabel("边：A"));
		pNorth.add(textFieldA);
		pNorth.add(new JLabel("边：B"));
		pNorth.add(textFieldB);
		pNorth.add(new JLabel("边：C"));
		pNorth.add(textFieldC);
		pNorth.add(controlButton);
		controlButton.addActionListener(this);
		add(pNorth, BorderLayout.NORTH);
		add(new JScrollPane(showArea), BorderLayout.CENTER);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		try {
			double a = Double.parseDouble(textFieldA.getText().trim());
			double b = Double.parseDouble(textFieldB.getText().trim());
			double c = Double.parseDouble(textFieldC.getText().trim());
			triangle.setA(a);
			triangle.setB(b);
			triangle.setC(c);
			String area = triangle.getArea();
			showArea.append("三角形" + a + "," + b + "," + c + "的面积:");
			showArea.append(area + "\n");
		} catch (Exception ee) {
			showArea.append("\n" + ee + "\n");
		}
	}
	
}
